<?php

/**
 * LoginController
 * 
 * @author Martin Bianculli
 * @version 0.1
 */
require_once 'Zend/Controller/Action.php';
require_once 'Click/Form/Login.php';

class LoginController extends Zend_Controller_Action {   
    
    /**
     * The default action
     */
    public function indexAction() {
        $this->_helper->_layout->setLayout('admin');
        $this->view->messages = $this->_helper->flashMessenger->getMessages();        
        $this->view->form = new Click_Form_Login();
    }

    public function authenticateAction() {        
        if ($this->getRequest()->isPost()) {
            $form = new Click_Form_Login(); 
            if ($form->isValid($this->getRequest()->getPost())) {            
                $email = $form->getValue('email');
                $password = $form->getValue('password');     
                if ($this->_authenticate($email, $password)) $this->_helper->redirector('view');            
            } else {                
                $this->_helper->FlashMessenger('¡Datos Incorrectos! Vuelva a intentar.');
            }
        }
        $this->_helper->redirector('index');
        
    }
    
    public function viewAction() {
        $user = Model_User::getLoggedUser();
        print_r($user); 
    }
    
    private function _authenticate($email, $password) {
        return Model_User::authenticate($email, $password)->isValid();        
    }
    
}
